#-------------------------------------------------------------------------------
# Name: CheckTDDSyntax
# Checks the syntax of the TDD file
# Author: rawat
#
# Created: 22-02-2011 Copyright: (c) rawat 2011 Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python


import sys
import TestCase

class CheckTDDSyntax(object):
	"""
	   Checks the syntax of the TDD file.
	   1. Names should not have spaces
	   2. DataType should be of given types - case sensitive
	   3. Facets should be of given types - case sensitive
	   4. Param should have data[] list size == 4.
	"""

	def __init__(self, attribs):
		self.Attributes = attribs
		#print "Number of attributes", len(self.Attributes)
		self.names = []
		self.types = []
		self.facets = []
		self.data = []
		self.refTypes = ["","tk_integer", "tk_boolean", "tk_double", "tk_string", "tk_specobject", "tk_component", \
					"tk_param_real", "tk_param_length", "tk_param_angle",	"tk_param_string", "tk_param_boolean", \
					"tklist_integer", "tklist_boolean", "tklist_double", "tklist_string",
					"tklist_specobject", "tklist_component",]
		self.refFacets = ["sp_IN", "sp_OUT", "sp_NEUTRAL"]
		for att in self.Attributes :
			name = att["name"]
			self.types.append(att["type"])
			self.names.append(att["name"])
			self.facets.append(att["facet"])
			self.data.append(att["data"])
			#print att
		#print "Types : ", self.types
		#print "Facets : ", self.facets

		return

	def CheckNames(self):
		""" Checks the name of the attribute """
		for att in self.Attributes :
			name = att["name"]
			if self.names.count(name) > 1 :
			   sys.stderr.write( "\nDuplicate attribute found " + name )
			   return -101
			if -1 != name.find(" ") :
			   sys.stderr.write("\nAttribute :" + name + " has space. Spaces are not allowed in attribute name. " )
			   return -102
			if len(name)  < 1:
			   sys.stderr.write( "\nEmpty Attribute " )
			   return -103
		#print names, len(names)
		return 0


	def CheckTypes(self):
		""" Checks the data type of the attribute """

		for i, typ in enumerate(self.types) :
			if self.refTypes.count(typ) < 1 :
			   sys.stderr.write( "\nAttribute : " + self.names[i] + ". Invalid type found : " +  typ)
			   sys.stderr.write( "\n\tType should be one of "+ str( self.refTypes))
			   return -201
		return 0

	def CheckFacet(self):
		""" Checks the facet of the attribute """

		for i, fac in enumerate(self.facets) :
			if self.refFacets.count(fac) < 1 :
			   sys.stderr.write("\nAttribute : " + self.names[i] + ". Invalid facet found " + str(fac) )
			   sys.stderr.write("\n\tFacet should be one of " + str(self.refFacets) )
			   return -301
		return 0

	def CheckParamAttr(self):
		""" Checks if the param attr have data of size four """

		for i in range( len(self.types)):
			if False == self.types[i].startswith("tk_param") :
			   continue

			if len(self.data[i]) != 4 :
			   sys.stderr.write("\nAttr " + self.names[i] + " data is only " + str(self.data[i]) )
			   sys.stderr.write("\n\ttk_param attr should have Data list of four elements " )
			   return -401

			if self.Attributes[i]["UI"] != "Param" :
			   sys.stderr.write("\nAttr " + self.names[i] + " UI is invalid : " + str(self.Attributes[i]["UI"]) )
			   sys.stderr.write("\n\tUI should be of type Param or empty string. " )
			   return -402

		return 0

	def Check(self):
		assert(self.CheckNames() == 0)
		assert(self.CheckFacet() == 0)
		assert(self.CheckTypes() == 0)
		assert(self.CheckParamAttr() == 0)
		return 0


if __name__ == '__main__':
   #main()
   #print sys.argv
   chk = CheckTDDSyntax( TestCase.Attributes )
   cmd="cmd=\"StateChart\""
   if TestCase.Context == "Part" or TestCase.Context == "Analysis" :
	cmd = "cmd=\"PrtPanel\""
	
   print cmd
   sys.exit(chk.Check())
